package com.gitee.wsl.doc.prosemirror.history.bean

import com.gitee.wsl.doc.prosemirror.state.bookmark.SelectionBookmark
import com.gitee.wsl.doc.prosemirror.transform.step.Step
import com.gitee.wsl.doc.prosemirror.transform.map.StepMap

class Item(
    // The (forward) step map for this item.
    val map: StepMap,
    // The inverted step
    val step: Step? = null,
    // If this is non-null, this item is the start of a group, and
    // this selection is the starting selection for the group (the one
    // that was active before the first step was applied)
    val selection: SelectionBookmark? = null,
    // If this item is the inverse of a previous mapping on the stack,
    // this points at the inverse's offset
    val mirrorOffset: Int? = null
) {

    fun merge(other: Item): Item? {
        if (this.step != null && other.step != null && other.selection == null) {
            val step = other.step.merge(this.step)
            if (step != null) {
                return Item(step.getMap().invert(), step, this.selection)
            }
        }
        return null
    }

    override fun toString(): String {
        return "Item(map=$map, step=$step, selection=$selection, mirrorOffset=$mirrorOffset)"
    }
}